package com.hatic.check;

import java.util.Map;

import org.jxstar.dao.DaoParam;
import org.jxstar.service.BusinessObject;
import org.jxstar.util.StringUtil;

import com.hatic.process.ProjectMoneyBO;

/**
 * 结算审计单处理类。
 *
 * @author TonyTan
 * @version 1.0, 2011-10-31
 */
public class BalanceBO extends BusinessObject {
	private static final long serialVersionUID = 1L;

	public String preAudit(String[] dataIds) {
		if (dataIds == null || dataIds.length == 0) {
			setMessage("没有选择审计记录！");
			return _returnFaild;
		}
		
		for (int i = 0, n = dataIds.length; i < n; i++) {
			if (!updateMoney(dataIds[i])) {
				return _returnFaild;
			}
		}
		
		return _returnSuccess;
	}
	
	//更新项目与计划中的结算金额
	private boolean updateMoney(String dataId) {
		Map<String,String> mpData = queryBalance(dataId);
		
		String projectId = mpData.get("project_id");
		double money = StringUtil.tonum(mpData.get("check_money"));
		
		//项目ID为空时，返回TRUE，即没有对应的项目，不做金额更新
		if(projectId == null || projectId.length() == 0) return true;
		
		ProjectMoneyBO moneybo = new ProjectMoneyBO();
		
		return moneybo.addCostMoney(projectId, money);
	}
	
	//查询审计信息
	private Map<String,String> queryBalance(String dataId) {
		String sql = "select * from sj_balance where sj_balanceid = ?";
		DaoParam param = _dao.createParam(sql);
		param.addStringValue(dataId);
		
		return _dao.queryMap(param);
	}
}
